﻿
function ShowMsg(thisObj, strMsg) {
    if (!thisObj)
        return;

    thisObj.attr("err", "err");
    var smal = thisObj.next("small");
    if (0 < smal.length) {
        $(smal[0]).text(strMsg).show();
    }
    else {
        thisObj.after($("<small></small>").text(strMsg).css({ color: "red" }));
    }
}

function ShowMsgAppendTo(thisObj, strMsg) {
    if (!thisObj)
        return;

    var smal = thisObj.children("small");
    if (0 < smal.length) {
        $(smal[0]).text(strMsg).show();
    }
    else {
        thisObj.appendTo($("<small></small>").text(strMsg).css({ color: "red" }));
    }
}

function ClearMsg(thisObj) {
    if (!thisObj)
        return;

    thisObj.removeAttr("err");
    var smal = thisObj.next("small");
    if (0 < smal.length) {
        $(smal[0]).hide();
    }
}

$("#useraccount").blur(function () {
    ClearMsg($("#useraccount"));
    var account = $("#useraccount").val();
    if (account.length < 4) {
        ShowMsg($("#useraccount"), "用户账号不能少于4个字符");
        return;
    }
    
    if (!/^[A-Za-z0-9_]*$/.test(account)) {
        ShowMsg($("#useraccount"), "仅支持字母,数字和下划线");
        return;
    }

    $.ajax({
        type: 'get',
        url: '/Register.ashx?action=CheckAccount&account=' + account,
        dataType: 'text',
        success: function (result) {
            if (result == "True") {
                ShowMsg($("#useraccount"), "该账号已经被注册过，请使用其他账号！！");
            }
        }
    });

});

$("#userpwd").blur(function () {
    var userpwd = $("#userpwd").val();
    ClearMsg($("#userpwd"));
    if (userpwd.trim() == "") {
        ShowMsg($("#userpwd"), "密码不能为空");
    }
});

$("#checkuserpwd").blur(function () {
    var userpwd = $("#checkuserpwd").val();
    ClearMsg($("#checkuserpwd"));
    if (userpwd.trim() == "") {
        ShowMsg($("#checkuserpwd"), "密码不能为空");
    }
    else {
        var userpwdff = $("#userpwd").val();
        if (userpwdff.trim() != "") {
            if (userpwdff != userpwd)
                ShowMsg($("#checkuserpwd"), "两次输入的密码不一致");
        }
    }
});

$("#username").blur(function () {
    var username = $("#username").val();
    var reg = /^[\u4e00-\u9fa5]{2,12}$/;
    ClearMsg($("#username"));
    if (!reg.test(username)) {
        ShowMsg($("#username"), "请输入至少两个中文字符");
        return false;
    }
    return true;
});
$("#bankname").blur(function () {
    var bankname = $("#bankname").val();
    ClearMsg($("#bankname"));
    var reg = /^[\u4e00-\u9fa5]{2,8}$/;
    if (!reg.test(bankname)) {
        ShowMsg($("#bankname"), "输入不正确!");
        return false;
    }
    return true;
});
$("#bankcard").blur(function () {
    var bankcard = $("#bankcard").val();
    ClearMsg($("#bankcard"));
    var reg = /^\d+$/g;   
    if (!reg.test(bankcard)) {
        ShowMsg($("#bankcard"), "格式错误,卡号为数字");
        return false;
    }
    else {
        $.ajax({
            type: 'post',
            url: '/Register.ashx?action=QueryEntityByBankID&bankcard=' + bankcard,
            dataType: 'text',
            success: function (result) {
                if (result == "True") {
                    ShowMsg($("#bankcard"), "该银行卡号已经被绑定");
                    return false;
                }
                return true;
            }
        });
        return true;
    }
});

function CheckIdCard(idCard) {
    ClearMsg($("#userIDcard"));
    var regIdCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    //如果通过该验证，说明身份证格式正确，但准确性还需计算
    if (regIdCard.exec(idCard)) {
        // return true;
        if (idCard.length == 18) {
            var idCardWi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //将前17位加权因子保存在数组里
            var idCardY = new Array(1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2); //这是除以11后，可能产生的11位余数、验证码，也保存成数组
            var idCardWiSum = 0; //用来保存前17位各自乖以加权因子后的总和
            for (var i = 0; i < 17; i++) {
                idCardWiSum += idCard.substring(i, i + 1) * idCardWi[i];
            }

            var idCardMod = idCardWiSum % 11;//计算出校验码所在数组的位置
            var idCardLast = idCard.substring(17);//得到最后一位身份证号码

            //如果等于2，则说明校验码是10，身份证号码最后一位应该是X
            if (idCardMod == 2) {
                if (idCardLast == "X" || idCardLast == "x") {
                    return true;
                } else {
                    ShowMsg($("#userIDcard"), "身份证号码错误");
                    return false;
                }
            } else {
                //用计算出的验证码与最后一位身份证号码匹配，如果一致，说明通过，否则是无效的身份证号码
                if (idCardLast == idCardY[idCardMod]) {
                    return true;
                } else {
                    ShowMsg($("#userIDcard"), "身份证号码错误");
                    return false;
                }
            }
        }
    } else {
        ShowMsg($("#userIDcard"), "身份证号码错误");
        return false;
    }
    return true;
}

$("#userIDcard").blur(function () {
    ClearMsg($("#userIDcard"));
    var idCard = $("#userIDcard").val();
    if (idCard == "") {
        ShowMsg($("#userIDcard"), "不能为空!");
        return false;
    }
    if (CheckIdCard(idCard)) {
        $.ajax({
            type: 'get',
            url: '/Register.ashx?action=QueryEntityByIdCard&idCard=' + idCard,
            dataType: 'text',
            success: function (result) {
                if (result == "True") {
                    ShowMsg($("#userIDcard"), "一个身份证号码只能注册一次!");
                    return false;
                }
                return true;
            }
        });
    }
});

function checkForm() {
    var list = $('.am-form-group input[err="err"]');
    if (!$('#cheBox').is(":checked")) {
        alert("请勾选 我已阅读《推广联盟用户服务协议》");
        return false;
    }

    if (0 != list.length) {
        alert("请输入有效信息!");
        return false;
    }
    else
        return true;
}

function OnClickGetCode() {
    var phone = $("#userPhone");
    if (phone.attr("err")) {
        alert("请输入正确手机号码");
        return;
    }

    if ((/^1[34578]\d{9}$/.test(phone.val()))) {
        var nTime = 60;
        //取消点击响应, 避免重复点击
        $("#sendCode").attr("onclick", "");
        //按钮不可用, 并进入60秒倒计时
        $("#sendCode").attr("disabled", true);
        var timeTer = setInterval(function (e) {
            --nTime;
            $("#sendCode").text("已发送! (" + nTime + " s)");
            if (0 == nTime) {
                clearInterval(timeTer);
                $("#sendCode").attr("disabled", false);
                $("#sendCode").attr("onclick", "OnClickGetCode()");
                $("#sendCode").text("获取验证码");
            }
        }, 1000);

        $.post("/PostMsg.aspx", "posttype=getcode&phoneNum=" + phone.val(), function (e) {
            if ("ok" == e) {
            }
            else {
                alert(e);
                $("#sendCode").attr("onclick", "OnClickGetCode()");
            }
        });
    }
    else {
        alert("输入有效手机号码");
    }
}

$("#userPhone").blur(function (e) {
    var phone = $(this);
    ClearMsg(phone);
    if ((/^1[34578]\d{9}$/.test(phone.val()))) {
        $.ajax({
            type: 'post',
            url: '/Register.ashx?action=ExistPhoneNumber&phone=' + phone.val(),
            dataType: 'text',
            success: function (result) {
                if (result == "True") {
                    ShowMsg(phone, "该手机号码已注册");
                }
            }
        });
    }
    else {
        ShowMsg(phone, "请输入有效手机号码");
    }
});

$("#phoneCheck").blur(function (e) {
    $(".am-g .cheCode small").hide();
    $("#phoneCheck").removeAttr("err");
    if (!/^\d{6}$/.test($("#phoneCheck").val())) {
        $("#phoneCheck").attr("err", "err");
        $(".am-g .cheCode small").show();
    }
});